const http = require('../../utils/http.js'); 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navindex: 0,
    userData: {},
    carData: {},
    page: 1,
    seek: '',
    carPage: 1,
    getData: {
      car: true,
      user: true
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData('',10,'');
  },
  onShow(){
    
  },
  // 搜索
  search() {
    http.postRequest('/mini/v1/car_owner/owner_list',{search: this.data.seek},res=> {
      this.setData({
        userData: res.data
      })
    })
  },
  // 双向数据流
  seekvalue(e) {
    this.setData({
      seek: e.detail.value
    })
  },
  // 下拉刷新
  onPullDownRefresh () {
    this.getData('','','');
    wx.stopPullDownRefresh();
  },
  // 到底部加载
  onReachBottom() {
    let page = this.data.page;
    if(this.data.userData.data.length && page<= this.data.userData.last_page && this.data.navindex == 0){
      page++;
      this.setData({
        page: page
      })
      this.getData('','',page);
    }
    let carPage = this.data.carPage;
    if(this.data.carData.data && carPage<= this.data.carData.last_page && this.data.navindex == 1) {
      carPage++;
      this.setData({
        carPage: carPage
      })
      this.getCardata('','',carPage);
    }
  },
  // 获取客户数据
  getData(search,per_page,page) {
    per_page = 15;
    http.postRequest('/mini/v1/car_owner/owner_list',{search: search,per_page: per_page, page: page},res=> {
      let userData = this.data.userData;
      if(userData.data) {
        for(let i in res.data.data) {
          userData.data.push(res.data.data[i]);
        }
      } else {
        userData = res.data
      }
      this.setData({
        userData: userData
      })
    })
  },
  // 获取车主数据
  getCardata(search,per_page,page) {
    http.postRequest('/mini/v1/car_owner/car_list',{search: search,per_page: per_page, page: page}, res=> {
      let carData = this.data.carData;
      if(carData.data) {
        for(let i in res.data.data) {
          carData.data.push(res.data.data[i]);
        }
      } else {
        carData = res.data
      }
      this.setData({
        carData: carData
      })
    })
  },
  // 搜索
  changInput(e){
    if(this.data.navindex == 0){
      http.postRequest('/mini/v1/car_owner/owner_list',{search: e.detail.value},res=> {
        let userData = this.data.userData;
        userData.data = res.data.data;
        this.setData({
          userData: userData,
          page: res.data.data.last_page
        })
      })
    } else if(this.data.navindex == 1) {
      http.postRequest('/mini/v1/car_owner/car_list',{search: e.detail.value},res=> {
        let carData = this.data.carData;
        carData.data = res.data.data;
        this.setData({
          carData: carData,
          carPage: res.data.data.last_page
        })
      })
    }
  },
  navindex(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      navindex: index
    })
    if(index) {
      this.getCardata('',10,'');
    } else {
      this.getData('',10,'');
    }
  },
  kehuxq(e) {
    let id = e.currentTarget.dataset.id;
    const pages = getCurrentPages()
    const prevPage = pages[pages.length-2] // 上一页
    // 调用上一个页面的setData 方法，将数据存储
    prevPage.setData({
      userid: id
    })
    wx.navigateBack({delta: 1});
  },
  gopage() {
    if(this.data.navindex == 0) {
      wx.navigateTo({
        url: '/pages/classify/child/newuser'
      })
    } else if(this.data.navindex == 1) {
      wx.navigateTo({
        url: '/pages/classify/child/newcar'
      })
    }
  },
  openCamera() {
    wx.navigateTo({
      url: '/pages/scan/scan'
    })
  }
})